package fy.hr.controller;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.controller.BaseController;

import fy.config.FyConfig;
import fy.form.listen.dao.MyOrgManager;
import fy.form.listen.log.LogReader;
import fy.hr.bean.MemberBean;
import fy.hr.dao.FindPersonInfoDao;
import fy.hr.dao.LeaveForm_FindUserInfoDao;
import fy.hr.dao.LeaveInstead_FindUserInfoDao;
import fy.hr.dao.MQPersonInfoDao;
import fy.hr.dao.QueryMemberInfoFromHRDao;
import fy.hr.dao.RecommendFormDao;
import fy.hr.dao.RecruitmentPersonInfoDao;
import fy.hr.dao.expertsDao;
import fy.hr.dao.positivelDao;
import fy.hr.manager.WritePersonalDevelopmentPlansManager;
import fy.hr.reinstatement.ReinstatementManager;
import fy.hr.staffRedeploy.StaffRedeploymentManager;
import fy.hr.staffRedeploy.StaffingNeedsManager;
import fy.util.MyUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;

public class OA2HRController extends BaseController {
	private static Log log = LogFactory.getLog(OA2HRController.class);
	private static final String findPersonInfoByCodeJSP = "fy_view/oa2hr/away/findPersonInfoByCode";
	private static final String writePersonInfoJSP = "fy_view/oa2hr/away/writePersonInfo";
	private static final String writePersonalDevelopmentPlans = "fy_view/oa2hr/writePersonalDevelopmentPlans";
	private static final String staffRedeploymentJSP = "fy_view/oa2hr/staffRedeployment";
	private static final String findJobJSP = "fy_view/oa2hr/findJob";
	private static final String findDetAndJobJSP = "fy_view/oa2hr/findDetAndJob";
	private static final String staffingNeedsJSP = "fy_view/oa2hr/staffingNeeds";
	private static final String recruitmentPersonInfoByIDnumJSP = "fy_view/oa2hr/Recruitment/recruitmentPersonInfo";
	private static final String workOverTimePersonInfoByIDnumJSP = "fy_view/oa2hr/workOvertime/workOverTimePersonInfo";
	private static final String JSONJSP = "fy_view/oa2hr/JSON";
	private static final String LOGJSP = "fy_view/oa2hr/log/log";
	private static final String LEAVEFROMJSP = "fy_view/oa2hr/leaveForm";

	// 专家
	private final static String findexpertsJSP = "fy_view/oa2hr/experts/findexperts";
	private final static String writeexpertsJSP = "fy_view/oa2hr/experts/writeexperts";
	// 转正
	private final static String findPositivelJSP = "fy_view/oa2hr/positive/findPositivel";
	private final static String writePositivelJSP = "fy_view/oa2hr/positive/writePositivel";

	public ModelAndView getStaffRedeploymentJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(staffRedeploymentJSP);

			StaffRedeploymentManager.initStaffRedeployment(modelAndView);
		}
		return modelAndView;
	}
    public ModelAndView findjobJSP(HttpServletRequest request,
								   HttpServletResponse response)throws Exception{
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(findJobJSP);

			//StaffRedeploymentManager.initStaffRedeployment(modelAndView);
		}
		return modelAndView;

	}

	public ModelAndView findDetAndJobJSP(HttpServletRequest request,
								   HttpServletResponse response)throws Exception{
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(findDetAndJobJSP);

			//StaffRedeploymentManager.initStaffRedeployment(modelAndView);
		}
		return modelAndView;

	}
	public ModelAndView querMemberInfoByCode(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(JSONJSP);
			response.setCharacterEncoding("utf-8");
			String code = request.getParameter("code");
			if ((code == null) || ("".equals(code))) {
				modelAndView.addObject("jsonStr", "1");
			} else {
				try {
					MemberBean bean = QueryMemberInfoFromHRDao
							.queryMemberByCode(code);
					modelAndView.addObject("jsonStr",
							bean == null ? "3" : bean.toString());
				} catch (Exception e) {
					modelAndView.addObject("jsonStr", "2");
					log.error("查询员工信息异常", e);
				}
			}
		}
		return modelAndView;
	}

	public ModelAndView staffRedeployment(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			String operate = request.getParameter("operate");
			String id = request.getParameter("id");
			modelAndView = new ModelAndView("fy_view/oa2hr/JSON");
			response.setCharacterEncoding("utf-8");
			modelAndView.addObject("jsonStr",
					StaffRedeploymentManager.queryDepInfo(operate, id));
		}
		return modelAndView;
	}

	public ModelAndView getStaffingNeedsJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(staffingNeedsJSP);
			String code = MyOrgManager.getInstance()
					.getMemberByID(AppContext.getCurrentUser().getId().longValue())
					.getCode();

			StaffingNeedsManager snm = new StaffingNeedsManager();

			snm.setMemberCode(code);

			snm.query();

			modelAndView.addObject("posts", snm.getPostOptions());
			modelAndView.addObject("postRankMap", snm.getPostRankMapJSON());
			modelAndView.addObject("postNumJSON", snm.getPostNumJSON());
		}
		return modelAndView;
	}

	public ModelAndView getFindPersonInfoJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(findPersonInfoByCodeJSP);
		}
		return modelAndView;
	}

	public ModelAndView getWritePersonalDevelopmentPlansJSP(
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = null;
		User user = AppContext.getCurrentUser();
		if (user != null) {
			WritePersonalDevelopmentPlansManager wm = new WritePersonalDevelopmentPlansManager();

			wm.setUserID(user.getId().longValue());

			wm.query();

			modelAndView = new ModelAndView(writePersonalDevelopmentPlans);

			modelAndView.addObject("post", wm.getPost());
			modelAndView.addObject("code", wm.getUserCode());
			modelAndView.addObject("unitshortname", wm.getUnitshortname());
			modelAndView.addObject("dep", wm.getDep());
		}
		return modelAndView;
	}

	public ModelAndView findPersonInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView(
				"fy_view/oa2hr/away/writePersonInfo");
		try {
			boolean success = false;

			String codes = request.getParameter("codes");

			String needDate = request.getParameter("needDate");

			String newAway = request.getParameter("newAway");

			response.setCharacterEncoding("utf-8");
			if ((AppContext.getCurrentUser() != null) && (codes != null)
					&& (!"".equals(codes))) {
				codes = codes.replaceAll(".*([';]+|(--)+).*", " ");
				if (!"".equals(codes)) {
					String[] codesArray = codes.split(",");
					if ((codesArray != null) && (codesArray.length != 0)) {
						FindPersonInfoDao fid = new FindPersonInfoDao();

						fid.setCodes(codesArray);
						fid.setNeedTime("true".equals(needDate));
						fid.setNewAway("true".equals(newAway));
						modelAndView.addObject("personInfoJSON", fid
								.findPersonInfo().toString());
						success = true;
					}
				}
			}
			if (!success) {
				modelAndView.addObject("personInfoJSON", "{}");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}

	public ModelAndView getErrorLogJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		User user = AppContext.getCurrentUser();
		if ((user != null) && ("zongban".equals(user.getLoginName()))) {
			modelAndView = new ModelAndView(LOGJSP);

			LogReader lr = new LogReader();

			lr.read(MyUtil.getRealPath(OA2HRController.class) + "../dao");
			lr.findFormInfo(FyConfig.getOA_DB_CONFIG());

			modelAndView.addObject("errorLog", lr.getLogJSON());
		}
		return modelAndView;
	}

	public ModelAndView getLeaveFormJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView(LEAVEFROMJSP);
		User user = AppContext.getCurrentUser();
		if (user != null) {
			LeaveForm_FindUserInfoDao dao = new LeaveForm_FindUserInfoDao();
			dao.setMemberID(user.getId().longValue());
			if (dao.query()) {
				modelAndView.addObject("userName", dao.getUserName());
				modelAndView.addObject("code", dao.getCode());
				modelAndView.addObject("post", dao.getPost());
				modelAndView.addObject("dep", dao.getDep());
				modelAndView.addObject("reportDate", dao.getReportDate());
				modelAndView.addObject("remainDays", dao.getRemainDays());
				modelAndView.addObject("nowTime", MyUtil.getNowStr());
				modelAndView.addObject("success", "true");
			}
		}
		return modelAndView;
	}

	public ModelAndView queryReinstatementInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		User user = AppContext.getCurrentUser();
		if (user != null) {
			modelAndView = new ModelAndView("fy_view/oa2hr/JSON");
			response.setCharacterEncoding("utf-8");
			String operate = request.getParameter("operate");
			if ("1".equals(operate)) {
				String memberCode = request.getParameter("memberCode");
				ReinstatementManager.queryInfo(modelAndView, user.getId()
						.longValue(), memberCode);
			} else {
				String pk = request.getParameter("pk");
				ReinstatementManager.queryDepAndPost(modelAndView, pk, operate);
			}
		}
		return modelAndView;
	}

	public ModelAndView queryRequirementInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView("fy_view/oa2hr/JSON");
		response.setCharacterEncoding("utf-8");
		User user = AppContext.getCurrentUser();
		if (user != null) {
			String number = request.getParameter("number");
			if ((number != null) && (number.length() == 11)) {
				modelAndView.addObject("jsonStr",
						RecommendFormDao.queryRequirementInfo(number));
			} else {
				modelAndView.addObject("jsonStr",
						"{success:false,data:'服务器获取的单据编号格式不对，请重试。'}");
			}
		} else {
			modelAndView.addObject("jsonStr",
					"{success:false,data:'未获取用户登录信息，请登录后再填写表单'}");
		}
		return modelAndView;
	}

	public ModelAndView queryRequirementPersonInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView("fy_view/oa2hr/JSON");
		response.setCharacterEncoding("utf-8");
		User user = AppContext.getCurrentUser();
		if (user != null) {
			String number = request.getParameter("number");
			if ((number != null)
					&& ((number.length() == 15) || (number.length() == 18))) {
				modelAndView.addObject("jsonStr",
						RecommendFormDao.queryRequirementPersonInfo(number));
			} else {
				modelAndView.addObject("jsonStr",
						"{success:false,data:'服务器获取的人员身份证格式不对，请重试。'}");
			}
		} else {
			modelAndView.addObject("jsonStr",
					"{success:false,data:'未获取用户登录信息，请登录后再填写表单'}");
		}
		return modelAndView;
	}

	public ModelAndView queryMemberInfo_Leave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView("fy_view/oa2hr/JSON");
		response.setCharacterEncoding("utf-8");
		User user = AppContext.getCurrentUser();
		if (user != null) {
			String code = MyOrgManager.getInstance()
					.getMemberByID(user.getId().longValue()).getCode();
			if (MyUtil.strIsNotNull(code)) {
				String leaveMemberCode = request.getParameter("code");
				if (MyUtil.strIsNotNull(leaveMemberCode)) {
					LeaveInstead_FindUserInfoDao dao = new LeaveInstead_FindUserInfoDao();
					dao.setInsteadCode(code);
					dao.setLeaveMemberCode(leaveMemberCode);
					modelAndView.addObject("jsonStr", dao.query());
					dao = null;
				} else {
					modelAndView.addObject("jsonStr",
							"{success:false,data:'请假人工号不能为空。'}");
				}
			} else {
				modelAndView
						.addObject("jsonStr",
								"{success:false,data:'代填人的OA信息不完整，缺失人员工号，请联系子公司信息部添加。'}");
			}
		} else {
			modelAndView.addObject("jsonStr",
					"{success:false,data:'未获得登录人员信息，请登录后重试。'}");
		}
		return modelAndView;
	}

	public ModelAndView getFindPersonInfoByCodeJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(workOverTimePersonInfoByIDnumJSP);
		}
		return modelAndView;
	}

	public ModelAndView jobNumFindPersonInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView(
				"fy_view/oa2hr/away/writePersonInfo");
		try {
			boolean success = false;
			String jobNums = request.getParameter("jobNums");
			response.setCharacterEncoding("utf-8");
			if ((AppContext.getCurrentUser() != null) && (jobNums != null)
					&& (!"".equals(jobNums))) {
				jobNums = jobNums.replaceAll(".*([';]+|(--)+).*", " ");
				if (!"".equals(jobNums)) {
					String[] jobNumsArray = jobNums.split(",");
					if ((jobNumsArray != null) && (jobNumsArray.length != 0)) {
						MQPersonInfoDao mq = new MQPersonInfoDao();
						mq.setCodes(jobNumsArray);

						modelAndView.addObject("personInfoJSON", mq
								.findPersonInfo().toString());
						success = true;
					}
					if (!success) {
						modelAndView.addObject("personInfoJSON", "{}");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}

	public ModelAndView getFindPersonInfoJSPbyidnum(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(recruitmentPersonInfoByIDnumJSP);
		}
		return modelAndView;
	}

	public ModelAndView idnumFindPersonInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView(writePersonInfoJSP);
		try {
			boolean success = false;
			String idnums = request.getParameter("idnums");

			response.setCharacterEncoding("utf-8");

			if (AppContext.getCurrentUser() != null && idnums != null
					&& !"".equals(idnums)) {

				RecruitmentPersonInfoDao rpd = new RecruitmentPersonInfoDao();
				modelAndView.addObject("personInfoJSON", rpd
						.idnumfindPersonInfo(idnums).toString());
				success = true;
			}
			if (!success) {
				modelAndView.addObject("personInfoJSON", "{}");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}

	// 专家测试
	public ModelAndView getCodeJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(findexpertsJSP);
		}
		return modelAndView;
	}

	public ModelAndView writeCodeJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		modelAndView = new ModelAndView(writeexpertsJSP);
		String code = request.getParameter("Code");
		response.setCharacterEncoding("utf-8");
		expertsDao cs = new expertsDao();//
		boolean success = false;
		if (AppContext.getCurrentUser() != null) {
			code = code.replaceAll(".*([';]+|(--)+).*", " ");
			String[] codeArray = code.split(",");
			cs.setCodeArray(codeArray);//
			modelAndView.addObject("CodeJSON", cs.csCode().toString());
			success = true;

		}
		if (!success) {
			modelAndView.addObject("CodeJSON", "{}");
		}
		return modelAndView;
	}

	// 转正
	public ModelAndView getpositivelJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(findPositivelJSP);
		}
		return modelAndView;
	}

	public ModelAndView writepositivelJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		modelAndView = new ModelAndView(writePositivelJSP);
		String code = request.getParameter("Code");
		response.setCharacterEncoding("utf-8");
		positivelDao pt = new positivelDao();//
		boolean success = false;
		if (AppContext.getCurrentUser() != null) {
			code = code.replaceAll(".*([';]+|(--)+).*", " ");
			String[] codeArray = code.split(",");
			pt.setCodeArray(codeArray);//
			modelAndView.addObject("PositivelJSON", pt.positive().toString());
			success = true;

		}
		if (!success) {
			modelAndView.addObject("PositivelJSON", "{}");
		}
		return modelAndView;
	}
}
